#! /usr/bin/env python
# -*- coding:utf-8 -*-

# mongodb
# https://www.cnblogs.com/melonjiang/p/6536876.html

from pymongo import MongoClient


conn = MongoClient('127.0.0.1',27017)

db = conn.mydb       # 链接mydb,没有自动创建
print('链接 mydb 数据库')
my_set = db.test_set # 使用test_set集合，没有自动创建
print('set')

# 插入
my_set.insert({"name":"zhangsan", "age":22})
print('insert插入一个列表多条数据不用遍历，效率高')
my_set.save({"name":"lisi", "age":18})
print('save需要遍历列表，一个个插入')
print('插入单条')
# 插入多条
# users = [{"name":"wanger","age":11},{"name":"mazi","age":18}]
# my_set.insert(users);
# users1 = [{"name":"wanger1","age":11},{"name":"mazi1","age":18}]
# my_set.save(users1);
# print('插入多条')


# 查询 查询不到 就是None
for i in my_set.find():
    print(i)

# 条件查询

for i in my_set.find({"name":"lisi"}):
    print(i)

print(my_set.find_one({"name":"zhangsan"}))


# 更新
my_set.update({"name":"zhangsan"},{'$set':{"age":20}})


#删除name=lisi的全部记录
my_set.remove({'name': 'zhangsan'})

#删除name=lisi的某个id的记录
id = my_set.find_one({"name":"lisi"})["_id"]
my_set.remove(id)

#删除集合里的所有记录
db.my_set.remove()
#my_set.remove()

print('删除所有')

for i in my_set.find():
    print(i)